﻿using System;
using System.IO;
using System.Linq;

namespace DNUtil
{
    public static class StreamUtil
    {
        public static bool StreamEquals(this Stream stream, Stream other)
        {
            const int bufferSize = 2048;
            byte[] buffer1 = new byte[bufferSize]; 
            byte[] buffer2 = new byte[bufferSize];
            while (true)
            {
                int count1 = stream.Read(buffer1, 0, bufferSize);
                int count2 = other.Read(buffer2, 0, bufferSize);

                if (count1 != count2)
                    return false;

                if (count1 == 0)
                    return true;

                if (!buffer1.Take(count1).SequenceEqual(buffer2.Take(count2)))
                    return false;
            }
        }
    }
}
